﻿using Lis.IDAL;
using Lis.KISSFRSTDAL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Text;

namespace Lis.DALFactory
{
    /// <summary>
    /// 由简单工厂转变抽象工厂
    /// </summary>
    public class AbstractDalFactory
    {
        //数据库访问驱动层实例所在的程序集
        private static readonly string assemblyName = ConfigurationManager.AppSettings["DalAssemblyName"];

        public static IUserInfoDal GetUserInfoDal()
        {
            //通过反射的方式来创建程序集，这就不需要创建实例了
            //从而就从“简单工厂”变化到“抽象工厂”
            //通过反射，加载对应程序集来创建数据库访问驱动层实例
            //IUserInfoDal dal = CallContext.GetData("UserInfoDal") as UserInfoDal;
            //if (dal == null)
            //{
            //    dal = new UserInfoDal();
            //    CallContext.SetData("UserInfoDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".UserInfoDal") as IUserInfoDal;
        }

        public static IBookInfoDal GetBookInfoDal()
        {
            //IBookInfoDal dal = CallContext.GetData("BookInfoDal") as BookInfoDal;
            //if (dal == null)
            //{
            //    dal = new BookInfoDal();
            //    CallContext.SetData("BookInfoDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".BookInfoDal") as IBookInfoDal;
        }

        public static IBookDetailDal GetBookDetailDal()
        {
            //IBookDetailDal dal = CallContext.GetData("BookDetailDal") as BookDetailDal;
            //if (dal == null)
            //{
            //    dal = new BookDetailDal();
            //    CallContext.SetData("BookDetailDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".BookDetailDal") as IBookDetailDal;
        }

        public static ICirculateDal GetCirculateDal()
        {
            //ICirculateDal dal = CallContext.GetData("CirculateDal") as CirculateDal;
            //if (dal == null)
            //{
            //    dal = new CirculateDal();
            //    CallContext.SetData("CirculateDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".CirculateDal") as ICirculateDal;
        }

        public static IUserStatusDal GetUserStatusDal()
        {
            //IUserStatusDal dal = CallContext.GetData("UserStatusDal") as UserStatusDal;
            //if (dal == null)
            //{
            //    dal = new UserStatusDal();
            //    CallContext.SetData("UserStatusDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".UserStatusDal") as IUserStatusDal;
        }

        public static IAppointmentInfoDal GetAppointmentInfoDal()
        {
            //IAppointmentInfoDal dal = CallContext.GetData("AppointmentInfoDal") as AppointmentInfoDal;
            //if (dal == null)
            //{
            //    dal = new AppointmentInfoDal();
            //    CallContext.SetData("AppointmentInfoDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".AppointmentInfoDal") as IAppointmentInfoDal;
        }

        public static IUserCirculateDal GetUserCirculateDal()
        {
            //IUserCirculateDal dal = CallContext.GetData("UserCirculateDal") as UserCirculateDal;
            //if (dal == null)
            //{
            //    dal = new UserCirculateDal();
            //    CallContext.SetData("UserCirculateDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".UserCirculateDal") as IUserCirculateDal;
        }

        public static ILogInfoDal GetLogInfoDal()
        {
            //ILogInfoDal dal = CallContext.GetData("LogInfoDal") as LogInfoDal;
            //if (dal == null)
            //{
            //    dal = new LogInfoDal();
            //    CallContext.SetData("LogInfoDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".LogInfoDal") as ILogInfoDal;
        }

        public static ICirculateInfoDal GetCirculateInfoDal()
        {
            //ICirculateInfoDal dal = CallContext.GetData("CirculateInfoDal") as CirculateInfoDal;
            //if (dal == null)
            //{
            //    dal = new CirculateInfoDal();
            //    CallContext.SetData("CirculateInfoDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".CirculateInfoDal") as ICirculateInfoDal;
        }

        public static IAppointmentDal GetAppointmentDal()
        {
            //IAppointmentDal dal = CallContext.GetData("AppointmentDal") as AppointmentDal;
            //if (dal == null)
            //{
            //    dal = new AppointmentDal();
            //    CallContext.SetData("AppointmentDal", dal);
            //}
            //return dal;
            return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".AppointmentDal") as IAppointmentDal;
        }

    }
}
